<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class OrderController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public function index()
    {
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        $map['id|cname'] = array($condition, $condition, '_multi' => true);

        // 获取所有分类
        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        if (I('cid')) {
            $cid = $map['cid'] = I('cid');
        }
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $post_object   = D('Admin/Order');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );

        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('订单列表') // 设置页面标题
            ->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮
            ->setSearch('请输入ID/联系人', U('index'))
            ->addTableColumn('id', 'ID')
            //->addTableColumn('postname', '活动名')
            ->addTableColumn('storename', '店名')
            ->addTableColumn('address', '地址')
            ->addTableColumn('cname', '联系人')
            ->addTableColumn('phone', '电话')
            ->addTableColumn('num', '场次')
            ->addTableColumn('plan', '策划','status')
            ->addTableColumn('show', 'intable秀','status')
            ->addTableColumn('record', '视频录制','status')
            ->addTableColumn('income', '网络课堂收益', 'status')
            ->addTableColumn('say', 'intable说','status')
            ->addTableColumn('kol', 'intable传播','status')
            ->addTableColumn('price', '金额')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            ->addRightButton('edit') // 添加编辑按钮
            
            ->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    /**
     * 新增文档
     * @author jry <598821125@qq.com>
     */
    public function add()
    {
        if (IS_POST) {
            $post_object = D('Admin/Order');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $order_object = D('Admin/Order');
            $module_object = M('admin_order');
            $menus        = $module_object->find($module_id);
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增订单') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                ->setExtraHtml('
    <script type="text/javascript">
    function countPrice() {
        otherPrice = $("#renshu").val()*$("#danjia").val();
        numPrice = $("#num").val()*1000;
        planPrice = $("#plan").is(":checked")?2000:0;
        sayPrice = $("#say").is(":checked")?1000:0;
        $("#price").val(otherPrice+numPrice+planPrice+sayPrice);
        
    }

    $(function(){

        $("#num").change(function() {
            countPrice();
        });
        $("#plan").change(function(){
            countPrice();
        });
        $("#say").change(function(){
            countPrice();
        });
        $("#renshu").keyup(function(){
            countPrice();
        });
        $("#danjia").keyup(function(){
            countPrice();
        });   

    });
    
           </script>     
                    ')
                //->addFormItem('post_id','select','活动名','活动名',select_list_as_tree('Admin/Post'))
                ->addFormItem('storename','text','店名','店名')
                ->addFormItem('address','text','地址','地址')
                ->addFormItem('cname','text','联系人','联系人')
                ->addFormItem('phone','text','电话','电话')
                ->addFormItem('renshu','text','品牌邀约客户数','品牌邀约客户数',array(),'','id=renshu')
                ->addFormItem('danjia','text','品牌邀约客户单价','品牌邀约客户单价',array(),'','id=danjia')
                ->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->addFormItem('plan', 'checkbox', '策划','策划', array('1' => '需要(2000元)'),'','id=plan')
                ->addFormItem('show', 'checkbox', 'intable秀','intable秀', array('1' => '需要'))
                ->addFormItem('record', 'checkbox', '视频录制','视频录制', array('1' => '需要'))
                ->addFormItem('income', 'checkbox', '网络课堂收益','网络课堂收益', array('1' => '需要'))
                ->addFormItem('say', 'checkbox', 'intable说','intable说', array('1' => '需要(1000元)'),'','id=say')
                ->addFormItem('kol', 'checkbox', 'intable传播','intable传播', array('1' => '需要'))
                ->addFormItem('price', 'text', '金额','金额',array(),'','id=price readonly=true')
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Order');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑订单') // 设置页面标题
                    ->setExtraHtml('
            <script type="text/javascript">
            function countPrice() {
                otherPrice = $("#renshu").val()*$("#danjia").val();
                numPrice = $("#num").val()*1000;
                planPrice = $("#plan").is(":checked")?2000:0;
                sayPrice = $("#say").is(":checked")?1000:0;
                $("#price").val(otherPrice+numPrice+planPrice+sayPrice);
                
            }

            $(function(){
                $("#num").change(function() {
                    countPrice();
                });
                $("#plan").change(function(){
                    countPrice();
                });
                $("#say").change(function(){
                    countPrice();
                });
                $("#renshu").keyup(function(){
                    countPrice();
                });
                $("#danjia").keyup(function(){
                    countPrice();
                });    

            });
            
                   </script>     
                            ')
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                //->addFormItem('post_id','select','活动名','活动名',select_list_as_tree('Admin/Post'))
                ->addFormItem('storename','text','店名','店名')
                ->addFormItem('address','text','地址','地址')
                ->addFormItem('cname','text','联系人','联系人')
                ->addFormItem('phone','text','电话','电话')
                ->addFormItem('renshu','text','品牌邀约客户数','品牌邀约客户数',array(),'','id=renshu')
                ->addFormItem('danjia','text','品牌邀约客户单价','品牌邀约客户单价',array(),'','id=danjia')
                ->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->addFormItem('plan', 'checkbox', '策划','策划', array('1' => '需要(2000元)'),'','id=plan')
                ->addFormItem('show', 'checkbox', 'intable秀','intable秀', array('1' => '需要'))
                ->addFormItem('record', 'checkbox', '视频录制','视频录制', array('1' => '需要'))
                ->addFormItem('income', 'checkbox', '网络课堂收益','网络课堂收益', array('1' => '需要'))
                ->addFormItem('say', 'checkbox', 'intable说','intable说', array('1' => '需要(1000元)'),'','id=say')
                ->addFormItem('kol', 'checkbox', 'intable传播','intable传播', array('1' => '需要'))
                ->addFormItem('price', 'text', '金额','金额',array(),'','id=price readonly=true')
                ->setFormData(D('Admin/Order')->find($id))
                ->display();
        }
    }

    // 将树形列表转换为树
    public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
    {
        //转换成树状列表(非严格模式)
        $tree = new \Util\Tree();
        $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);

        if ($extra) {
            $result[0] = $extra;
        }

        //转换成一维数组
        foreach ($list as $val) {
            $result[$val[$key]] = $val['title_show'];
        }
        return $result;
    }
}
